<?php

class ModuleResultOfMemberStat extends CmsModule {
    //set the public head
    private function setHeadString($condition=null,$sort_by=null,$rand_code) {
        $date_list = null;
        $current_time = date('Y-m-d H:i:s');

        /**
        if (!empty($condition) && is_array($condition)) {
            //date time.
            $date_list = '<strong>By '.ucwords($condition['date_by']).': </strong>&nbsp; '.$condition['date_time'];
        }*/


        $condition_list = '<br/>'.
                          '<div>'.$date_list.'</div>';

       //control tag.                         
        $export_tag =  'Export to '.AC::exportHtmlControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_MEMBER_STAT)
                                   .AC::exportPdfControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_MEMBER_STAT)
                                   .AC::exportCvsControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_MEMBER_STAT);

        $head_string = '<div class="hd">'.
                       '    <div class="utility">'.
                       '        <a href="#" class="print">Print</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$export_tag.
                       '    </div>'.
                       '    <hr>'.
                       '    <h1>'.$this->title.'</h1>'.
                       '    <div class="ui-helper-clearfix">'.
                       '        <div class="time">'.$current_time.'</div>'.
                       '        <ul>'.$condition_list.'</ul>'.
                       '    </div>'.
                       '</div>';

        $head_string_exprot = '<div class="hd">'.
                                '    <div class="utility">'.
                                '        <a href="#" class="print">Print</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.
                                '    </div>'.
                                '    <hr>'.
                                '    <h1>'.$this->title.'</h1>'.
                                '    <div class="ui-helper-clearfix">'.
                                '        <div class="time">'.$current_time.'</div>'.
                                '        <ul>'.$condition_list.'</ul>'.
                                '    </div>'.
                                '</div>';

        return array('view'=>$head_string,'export'=>$head_string_exprot);
    }    

    //out put the body string.
    private function getOutPutBody($head_string,$body_string) {
        $output ='<div id="result" class="members_stat_result" >'.$head_string.
                 '  <div class="bd">'.
                 '  <br />'.
                 '  <table width="100%">'.
                    $body_string.
                 '  </table>'.
                 '  </div>'.
                 '</div>';
        return $output;
    }
    
    //get the page search filter condition.
    private function getPageCondition($params) {
        $date_by = (!empty($params['dateby'])) ? $params['dateby'] : 'month';
        
        switch($date_by) {
            case 'month':
                $date_time = $params['year'];
                break;
            case 'date':
                $date_time = $params['year'].'-'.$params['month'];
                break;
        }

        return array('condition'=>array('date_by'=>$date_by,'date_time'=>$date_time));
    }
    
    private function setHeaderString ($condition) {
        return null;
    }

    //set the body string.
    private function setBodyString($date_time,$date_by,$data) {
        $new_member_precent = ($data['new_member']==0) ? 0  : round(($data['new_member']/$data['all_member'])*100,2);
        $new_member_login_precent = ($data['new_member_login']==0) ? 0 : round(($data['new_member_login']/$data['all_member'])*100,2);
        $old_member_precent = ($data['old_member']==0) ? 0 : round(($data['old_member']/$data['all_member'])*100,2);
        $old_member_active_precent = ($data['old_member_active']==0) ? 0 : round(($data['old_member_active']/$data['all_member'])*100,2);
        $new_reg_precent = ($data['new_registration']==0) ? 0 : round(($data['new_registration']/$data['all_member'])*100,2);
        $register_no_confirm_precent = ($data['registered_no_confirm']==0) ? 0 : round(($data['registered_no_confirm']/$data['all_member'])*100,2);

        $body_list ='<tr class="weight" bgcolor="#CCC">'.
                    '   <td width="20%">&nbsp;</td>'.
                    '   <td align="left">統計列表</td>'.
                    '   <td width="10%" align="right">人數</td>'.
                    '   <td align="right">百分比</td>'.
                    '   <td width="20%">&nbsp;</td>'.
                    '</tr>'.
                    '<tr>'.
                    '   <td>&nbsp;</td>'.
                    '   <td align="left">會員總數:&nbsp;</td>'.
                    '   <td align="right">'.number_format($data['all_member']).'</td>'.
                    '   <td></td>'.
                    '   <td width="20%">&nbsp;</td>'.
                    '</tr>'.
                    '<tr>'.
                    '   <td>&nbsp;</td>'.
                    '   <td align="left">新會員人數</td>'.
                    '   <td align="right">'.number_format($data['new_member']).'</td>'.
                    '   <td align="right"><b>'.$new_member_precent.'%</b></td>'.
                    '   <td width="20%">&nbsp;</td>'.
                    '</tr>'.
                    '<tr>'.
                    '   <td>&nbsp;</td>'.
                    '   <td align="left">新會員有登錄過的人數</td>'.
                    '   <td align="right">'.number_format($data['new_member_login']).'</td>'.
                    '   <td align="right"><b>'.$new_member_login_precent.'%</b></td>'.
                    '   <td width="20%">&nbsp;</td>'.
                    '</tr>'.
                    '<tr>'.
                    '   <td>&nbsp;</td>'.
                    '   <td align="left">舊會員人數</td>'.
                    '   <td align="right">'.number_format($data['old_member']).'</td>'.
                    '   <td align="right"><b>'.$old_member_precent.'%</b></td>'.
                    '   <td width="20%">&nbsp;</td>'.
                    '</tr>'.
                    '<tr>'.
                    '   <td>&nbsp;</td>'.
                    '   <td align="left">舊會員導入後有登入過的人數</td>'.
                    '   <td align="right">'.number_format($data['old_member_active']).'</td>'.
                    '   <td align="right"><b>'.$old_member_active_precent.'%</b></td>'.
                    '   <td width="20%">&nbsp;</td>'.
                    '</tr>'.
                    '<tr>'.
                    '   <td>&nbsp;</td>'.
                    '   <td align="left">新注冊人數</td>'.
                    '   <td align="right">'.number_format($data['new_registration']).'</td>'.
                    '   <td align="right"><b>'.$new_reg_precent.'%</b></td>'.
                    '   <td width="20%">&nbsp;</td>'.
                    '</tr>'.
                    '<tr>'.
                    '   <td>&nbsp;</td>'.
                    '   <td align="left">已注冊人數(未確認)</td>'.
                    '   <td align="right">'.number_format($data['registered_no_confirm']).'</td>'.
                    '   <td align="right"><b>'.$register_no_confirm_precent.'%</b></td>'.
                    '   <td width="20%">&nbsp;</td>'.
                    '</tr>';
        return $body_list;
    }
    
    private function getBodyDataList($condition) {
        $date_by = $condition['date_by'];
        $date_time = $condition['date_time'];
        $obj = new ModelReportPages();
        $data = $obj->getMemberStatData($date_by,$date_time);
        return $data;
    }


   
    private function setMstatInlineMemberStatBodyString($condition) {
        $body_list = $this->setHeaderString($condition['date_by']);
        $data = $this->getBodyDataList($condition);
        $body_list .= $this->setBodyString($condition['date_time'],$condition['date_by'],$data);
        return $body_list;
    }


    //entrance.
    protected function __renderHead() {
        $body_string = null;
        $rand_code = rand(10000,99999);

        //parameters re-assignment.
        $conditions = $_GET;
        $conditions = $this->getPageCondition($conditions);

        //get the header string.
        $head_string = $this->setHeadString($conditions['condition'],null,$rand_code);
        
        //body string.
        $body_string .= $this->setMstatInlineMemberStatBodyString($conditions['condition']);

        //write a tmp file Export Tool.
        $export_html_string = $this->getOutPutBody($head_string['export'],$body_string);
        $write_result = AC::getInstance()->writeTempBodyToFile($rand_code,$export_html_string);
        
        //output.
        $this->contents =$this->getOutPutBody($head_string['view'],$body_string);
        
    }
}

?>